
package com.bear.cmcc.NetTestHTTP.ui.adapter;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.VillageBean;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityGridData;
import com.bear.cmcc.NetTestHTTP.ui.view.HVListView;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class GridSortAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;

    private boolean isOpenSelect = true;

    private ArrayList<CityGridData> list;

    private ViewHolder viewholder;

    private boolean[] isCheckState;

    private HVListView mListView;

    public GridSortAdapter(Context context, ArrayList<CityGridData> list, HVListView mListView) {
        this.list = list;
        this.mListView = mListView;
        layoutInflater = LayoutInflater.from(context);
    }

    public void notifyDataSetChanged(int lenght) {
        checkStateInit(lenght);
        super.notifyDataSetChanged();
    }

    private void checkStateInit(int lenght) {
        isCheckState = null;
        isCheckState = new boolean[lenght];
        for (int i = 0; i < isCheckState.length; i++) {
            isCheckState[i] = false;
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        CityGridData csb = list.get(position);
        viewholder = null;
        if (view == null) {
            viewholder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.item_grid_netinfo, null);
            viewholder.lyItem = (LinearLayout) view.findViewById(R.id.testItem);
            viewholder.cbSelect = (CheckBox) view.findViewById(R.id.cbSelect);
            viewholder.cbSelect.setClickable(false);
            viewholder.tvNumber = (TextView) view.findViewById(R.id.tvNumber);
            viewholder.tvCellName = (TextView) view.findViewById(R.id.tvCellName);
            viewholder.tvSpeed = (TextView) view.findViewById(R.id.tvSpeed);
            viewholder.tvDropRate = (TextView) view.findViewById(R.id.tvDropRate);
            viewholder.tvSignalValue = (TextView) view.findViewById(R.id.tvSignalValue);
            // viewholder.tvCalValue = (TextView)
            // view.findViewById(R.id.tvCalValue);
            viewholder.tvJietong = (TextView) view.findViewById(R.id.tvjietong);
            viewholder.lyItem.setMinimumHeight(60);
            viewholder.lyItem.setGravity(Gravity.CENTER);
            view.setTag(viewholder);

        } else {
            viewholder = (ViewHolder) view.getTag();
        }
        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.bg_flutter_list);
        } else {
            view.setBackgroundResource(R.drawable.black_background);
        }

        viewholder.cbSelect.setChecked(isCheckState[position]);

        if (isOpenSelect) {
            viewholder.cbSelect.setVisibility(View.VISIBLE);
        } else {
            viewholder.cbSelect.setVisibility(View.GONE);
        }

        viewholder.tvNumber.setText(String.valueOf(position + 1));
        viewholder.tvCellName.setText(csb.getGridName());
        viewholder.tvSpeed.setText(String.valueOf(csb.getAverageSpeed()));
        viewholder.tvDropRate.setText(csb.getFailRate());
        viewholder.tvSignalValue.setText(String.valueOf(csb.getSignalStrength()));
        // viewholder.tvCalValue.setText(String.valueOf(csb.getLac()));
        viewholder.tvJietong.setText(csb.getConnectionRate());

        // 校正（处理同时上下和左右滚动出现错位情况）
        View child = ((ViewGroup) view).getChildAt(1);
        int head = mListView.getHeadScrollX();
        if (child.getScrollX() != head) {
            child.scrollTo(mListView.getHeadScrollX(), 0);
        }
        return view;
    }

    class ViewHolder {
        LinearLayout lyItem;

        CheckBox cbSelect;

        TextView tvNumber;

        TextView tvCellName;

        TextView tvSpeed;

        TextView tvDropRate;

        TextView tvSignalValue;

        // TextView tvCalValue;
        TextView tvJietong;
    }

    /**
     * 是否打开选择
     * 
     * @param isbool true打开，false关闭
     */
    public void setSelect(boolean isbool) {
        isOpenSelect = isbool;
        super.notifyDataSetChanged();
    }

    /**
     * 是否全选
     * 
     * @param isbool true选中，false取消
     */
    public void setAllSelect(boolean isbool) {
        for (int i = 0; i < isCheckState.length; i++) {
            isCheckState[i] = isbool;
        }
        super.notifyDataSetChanged();
    }

    /**
     * 单选
     * 
     * @param location 单选下标
     */
    public void setRadioSelect(int location) {
        if (isCheckState[location]) {
            isCheckState[location] = false;
        } else {
            isCheckState[location] = true;
        }
        super.notifyDataSetChanged();
    }

    /**
     * 是否选中
     * 
     * @param location 行号
     * @return true选中，false未选
     */
    public boolean isSelected(int location) {
        return isCheckState[location];
    }
}
